package com.itheima.mall.dubbo.api.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.commons.utils.StringUtils;
import com.itheima.mall.dubbo.mapper.sms.HomeAdvertiseMapper;
import com.itheima.mall.entity.domain.sms.HomeAdvertise;
import com.itheima.mall.entity.domain.sms.HomeBrand;
import com.itheima.mall.entity.domain.vo.smsre.ResultAll;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@DubboService
public class HomeAdvertiseServiceImpl extends ServiceImpl<HomeAdvertiseMapper, HomeAdvertise> implements HomeAdvertiseService {
    @Autowired
    private HomeAdvertiseMapper homeAdvertiseMapper;

    @Override
    public ResultAll page(String endTime, String name, Integer pageNum, Integer pageSize, Integer type) {

//        Page<HomeAdvertise> homeAdvertisePage = new Page<>(pageNum, pageSize);
//        ResultAll resultAll = new ResultAll();
//        LambdaQueryWrapper<HomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(StringUtils.isNotEmpty(name), HomeAdvertise::getName, name);
//        queryWrapper.eq(type != null, HomeAdvertise::getType, type);
//        //queryWrapper.eq(endTime != null, HomeAdvertise::getEndTime, endTime);
//        homeAdvertiseMapper.selectPage(homeAdvertisePage, queryWrapper);
//        resultAll.setList(homeAdvertisePage.getRecords());
//        resultAll.setPageNum(pageNum);
//        resultAll.setPageSize(pageSize);
//        resultAll.setTotal(homeAdvertisePage.getTotal());
//        resultAll.setTotalPage(homeAdvertisePage.getPages());
//        return resultAll;

        Page<HomeAdvertise> homeAdvertisePage = new Page<>(pageNum, pageSize);
        ResultAll resultAll = new ResultAll();
        LambdaQueryWrapper<HomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), HomeAdvertise::getName, name);
        queryWrapper.eq(type != null, HomeAdvertise::getType, type);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime parse = null;
        if(endTime != null) {
            parse = LocalDate.parse(endTime, df).atStartOfDay();
        }
        queryWrapper.ge(endTime != null, HomeAdvertise::getEndTime, parse);
        homeAdvertiseMapper.selectPage(homeAdvertisePage, queryWrapper);
        resultAll.setList(homeAdvertisePage.getRecords());
        resultAll.setPageNum(pageNum);
        resultAll.setPageSize(pageSize);
        resultAll.setTotal(homeAdvertisePage.getTotal());
        resultAll.setTotalPage(homeAdvertisePage.getPages());
        return resultAll;
    }

    @Override
    public void updateSort(Long id, Integer status) {
        HomeAdvertise homeAdvertise = new HomeAdvertise();
        homeAdvertise.setId(id);
        homeAdvertise.setStatus(status);
        homeAdvertiseMapper.updateById(homeAdvertise);
    }
}
